<template>
  <div class="edit-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserInfo } from "@/api/user";
import dayjs from 'dayjs'

export default {
  name: "EditBirthday",
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 月份0 代表1月
      minDate: new Date(1930, 0, 1),
      maxDate: new Date(),
      currentDate: this.value ? new Date(this.value) : new Date(),
    };
  },
  computed: {},
  watch: {},
  methods: {
    async onConfirm(value) {
      console.log('选中的日期',value);
      this.$toast({
        message: "修改中...",
        forbidClick: true
      })
      // const date = `${this.currentDate.getFullYear()}-${this.currentDate.getMonth()+1}-${this.currentDate.getDate()}`;
      const date = dayjs(this.currentDate).format('YYYY-MM-DD');
      try {
       const {data} = await updateUserInfo({
         birthday: date
       });
       console.log(data);
       this.$toast.success('保存成功')
      } catch (err) {
        if (err && err.response && err.response.status) {
          // console.log('修改失败',err.response.status);
        }
      }
      // this.$emit('update:birthday', value)
      this.$emit('input',date)
      this.$emit('close')
    }
  },
  created() {},
  mounted() {},
};
</script>

<style lang="less" scoped>
//@import url(); 引入公共css类
</style>
